package com.hndzyu.yd.team;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.bean.huoban;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.widget.MyListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zz9527 on 2019/1/18.
 */
public class ZhiShuActivity extends Activity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.backarea)
    RelativeLayout backarea;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.ll11)
    LinearLayout ll11;
    @BindView(R.id.team_iv)
    ImageView teamIv;
    @BindView(R.id.team_name)
    TextView teamName;
    @BindView(R.id.team_phone)
    TextView teamPhone;
    @BindView(R.id.ll2)
    LinearLayout ll2;
    @BindView(R.id.lv)
    MyListView lv;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.total)
    TextView total;
    private ZhiShuAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhishu);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarColor(R.color.them)
                .fitsSystemWindows(true).init();
        backarea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        adapter = new ZhiShuAdapter(this);
        lv.setAdapter(adapter);
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                refreshLayout.finishRefresh(2000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                refreshLayout.finishLoadMore(2000);
            }
        });
        getData();
    }

    int page = 1;

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.ZHISHU(UserBean.uid, page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                try {


                    Gson gson = new Gson();
                    huoban huoban = gson.fromJson(json, huoban.class);
                    final huoban.LeaderBean leader = huoban.getLeader();
                    if (leader != null) {
                        ll1.setVisibility(View.VISIBLE);
                        ll11.setVisibility(View.VISIBLE);
                        Glide.with(ZhiShuActivity.this).load(Path.img(leader.getAvatar())).apply(new RequestOptions().circleCrop()).into(teamIv);
                        teamName.setText("ID:" + leader.getUid());
                        teamPhone.setText(leader.getMobile());
                        teamPhone.setOnLongClickListener(
                                new View.OnLongClickListener() {
                                    @Override
                                    public boolean onLongClick(View view) {
                                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                                        cm.setText(leader.getMobile());
                                        Toast.makeText(ZhiShuActivity.this, "复制成功", Toast.LENGTH_SHORT).show();
                                        return false;
                                    }
                                }
                        );
                    }
                    List<huoban.OneBean.DataBean> data = huoban.getOne().getData();
                    if (data.size() > 0) {
                        ll2.setVisibility(View.VISIBLE);
                        total.setText(Html.fromHtml("共<font color=\"#FF0000\">" + huoban.getOne().getTotal() + "</font>人"));
                        adapter.upData(data, page);
                    } else {
                        Toast.makeText(ZhiShuActivity.this, "没有更多了", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(ZhiShuActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
